package com.tv189.springbatchtest.steplisten;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * @ClassName: StepListenerJob
 * @Description: 步骤监听器任务
 * @Author: 19202
 * @Date: 2025/5/28 21:53
 * @Version 1.0
 **/
@SpringBootApplication
@EnableBatchProcessing
public class StepListenerJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet tasklet() {
        return (stepContribution, chunkContext) -> {
            System.out.println("----->" + System.currentTimeMillis());
            return RepeatStatus.FINISHED;
        };
    }

    @Bean MyStepListener myStepListener() {
        return new MyStepListener();
    }

    @Bean
    public Step step(){
        return stepBuilderFactory.get("step")
                .tasklet(tasklet())
                .listener(myStepListener())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job")
                .start(step())
                .incrementer(new RunIdIncrementer())
                .build();
    }


    public static void main(String[] args) {
        SpringApplication.run(StepListenerJob.class, args);
    }
}
